<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2020/12/1
 * Time: 15:59
 */
namespace App\Service\Admin;

use App\Http\Model\Mysql\BoxAreaModel;
use App\Http\Model\Mysql\BoxOwnerModel;
use Carbon\Carbon;
use Illuminate\Support\Facades\Hash;

class OwnerService extends BaseService
{
    /**
     * 盒子用户列表
     * @param array $query
     * @param int|null $limit
     * @param int $page
     * @return array
     */
    public function ownerList(array $query, int $limit = NULL, int $page = 1)
    {
        $limit  =   empty($limit) ? self::PAGE_LIMIT : $limit;
        $model  =   BoxOwnerModel::query();

        if (!empty($query['phone'])) {
            $model  =   $model->where(['phone' => $query['phone']]);
        }

        if (!empty($query['name'])) {
            $model  =   $model->where(['name' => $query['name']]);
        }

        if (!empty($query['parent_id'])) {
            $model  =   $model->where(['parent_id' => $query['parent_id']]);
        }

        $count  =   $model->count();
        $list   =   $model->offset(($page - 1) * $limit)->limit($limit)->select(['id', 'unique_id', 'name', 'nick_name', 'phone', 'box_count', 'level', 'parent_id'])
                    ->get()->each(function (&$item) {
                        $item->parent_name  =   BoxOwnerModel::where(['id' => $item->parent_id])->value('nick_name') ?? '';
                    });

        if ($list->isEmpty()) {
            return [true, []];
        } else {
            return [true, [
                'count' =>  $count,
                'list'  =>  $list->toArray(),
                'pages' =>  ceil($count / $limit),
                'current'=> $page
            ]];
        }
    }

    /**
     * 盒子用户添加
     * @param array $params
     * @return array
     */
    public function ownerAdd(array $params)
    {
        $owner['user_id']   =   $params['user_id'] ?? 0;
        $owner['name']      =   $params['name'];
        $owner['nick_name'] =   $params['nick_name'];
        $owner['phone']     =   $params['phone'];
        $owner['unique_id'] =   createUniqueID();
        $owner['password']  =   Hash::make('12345678');
        $owner['parent_id'] =   $params['parent_id'] ?? 0;
        /*if ($owner['parent_id'] != 0) {
            $level  =   (BoxOwnerModel::where(['id' => $owner['parent_id']])->value('level') ?? 0) + 1;
        } else {
            $level  =   1;
        }*/
        $owner['level']     =   $params['level'];
        $owner['desc']      =   $params['desc'] ?? '';

        $result =   BoxOwnerModel::create($owner);
        if ($result) {
            return [true, '添加成功'];
        } else {
            return [false, '添加失败'];
        }
    }

    /**
     * 修改盒子用户
     * @param array $params
     * @param int $owner_id
     * @return array
     */
    public function ownerEdit(array $params, int $owner_id)
    {
        $owner['user_id']   =   $params['user_id'] ?? 0;
        $owner['name']      =   $params['name'];
        $owner['nick_name'] =   $params['nick_name'];
        $owner['phone']     =   $params['phone'];
        /*$owner['parent_id'] =   $params['parent_id'] ?? 0;
        if ($owner['parent_id'] != 0) {
            $level  =   (BoxOwnerModel::where(['id' => $owner['parent_id']])->value('level') ?? 0) + 1;
        } else {
            $level  =   1;
        }*/
        $owner['level']     =   $params['level'];
        $owner['desc']      =   $params['desc'] ?? '';

        $result =   BoxOwnerModel::where(['id' => $owner_id])->update($owner);
        if ($result) {
            return [true, '修改成功'];
        } else {
            return [false, '修改失败'];
        }
    }

    /**
     * 删除盒子用户
     * @param int $owner_id
     * @return array
     */
    public function ownerDelete(int $owner_id)
    {
        $owner_info =   BoxOwnerModel::where(['id' => $owner_id])->select(['id', 'level', 'box_count', 'parent_id'])->first();
        if (empty($owner_info)) {
            return [false, '盒子用户不存在'];
        }

        if (BoxOwnerModel::where(['parent_id' => $owner_id])->exists()) {
            return [false, '有下级用户，无法删除'];
        }

        if ($owner_info['box_count'] > 0) {
            return [false, '有盒子，无法删除'];
        }

        $result =   BoxOwnerModel::where(['id' => $owner_id])->delete();
        if ($result) {
            return [true, '删除成功'];
        } else {
            return [false, '删除失败'];
        }
    }

    /**
     * 盒子用户信息
     * @param int $owner_id
     * @return array
     */
    public function ownerInfo(int $owner_id)
    {
        $owner_info =   BoxOwnerModel::where(['id' => $owner_id])->select(['id', 'level', 'box_count', 'parent_id', 'name', 'nick_name', 'phone', 'desc', 'unique_id'])->first();
        if (empty($owner_info)) {
            return [false, '盒子用户不存在'];
        }

        $owner_info->parent_name    =   BoxOwnerModel::where(['id' => $owner_info->parent_id])->value('name') ?? '';
        return [true, $owner_info->toArray()];
    }
}
